package com.wandian.games.mvvm.gamehome;

import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.mylib.libcore.AppContext;
import com.mylib.libcore.mvvm.BaseViewModel;
import com.mylib.libcore.network.rx.RxResultHelper;
import com.mylib.libcore.utils.JsonUtil;
import com.mylib.libcore.utils.MD5Util;
import com.mylib.libcore.utils.DevicesIDUtil;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.games.bean.DuoYouBaseBean;
import com.wandian.games.bean.ToDayGameDataBean;
import com.wandian.games.bean.XWGameDataBean;
import com.wandian.games.bean.YWHZGameTaskDataBean;
import com.wandian.games.data.BaseAppApi;
import com.wandian.games.tools.DUOYOUSdkSign;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static com.mylib.libcore.config.AppConfig.DUOYOU_APP_ID;
import static com.mylib.libcore.config.AppConfig.XIANWANG_AppSecret;
import static com.mylib.libcore.config.AppConfig.XIANWAN_APP_ID;
import static com.mylib.libcore.config.AppConfig.YWHZ_APP_ID;
import static com.mylib.libcore.config.AppConfig.YWHZ_AppSecret;

/**
 * @author Administrator
 * @date 2019/12/16-13:32
 * @description GamesHomeFragmentViewModel
 **/
public class GamesHomeFragmentViewModel extends BaseViewModel {
    public boolean haveMore =  true;
    public MutableLiveData<List<ToDayGameDataBean>> listTodayBean =  new MutableLiveData<>();
    public GamesHomeFragmentViewModel(@NonNull Application application) {
        super(application);
    }

//
//    //闲玩
//    public void getXWList(Context mContext){
//        //创建Retrofit对象
//        Retrofit retrofit = new Retrofit.Builder()
//                .baseUrl("https://h5.17xianwan.com/") //基础url,其他部分在GetRequestInterface里
//                .addConverterFactory(GsonConverterFactory.create()) //Gson数据转换器
//                .build();
//        //创建网络请求接口实例
//        BaseAppApi request = retrofit.create(BaseAppApi.class);
//        HashMap<String, String> hashMap = new HashMap<>();
//        hashMap.put("ptype","2");
//        hashMap.put("androidosv", Build.VERSION.SDK_INT+"");
//        hashMap.put("appid",XIANWAN_APP_ID);
//        hashMap.put("msaoaid", DevicesIDUtil.getOaid());
//        String imid = DevicesIDUtil.getIMEI(mContext);
//        if(UIUtils.isEmpty(imid)){
//            imid="0";
//        }
//        hashMap.put("deviceid", imid);
//        hashMap.put("appsign", AppContext.getInstance().getAppPreferencesHelper().getKeyValue("user_id"));
//        hashMap.put("keycode", MD5Util.Md5String(XIANWAN_APP_ID+imid+ DevicesIDUtil.getOaid()+Build.VERSION.SDK_INT+"2"+
//                AppContext.getInstance().getAppPreferencesHelper().getKeyValue("user_id") +XIANWANG_AppSecret));
//        hashMap.put("page","1");
//        hashMap.put("pagesize","5");
//        Call<Object> call = request.gateXWData(hashMap);
//        //发送网络请求(异步)
//        call.enqueue(new Callback<Object>() {
//            @Override
//            public void onResponse(Call<Object> call, Response<Object> response) {
//                Log.d("game=2", "xw="+ JsonUtil.toJson(response));
//                XWGameDataBean bean = (XWGameDataBean)JsonUtil.parseJsonToBean(JsonUtil.toJson(response.body()),XWGameDataBean.class);
//                if(!UIUtils.isEmpty(bean) && !UIUtils.isEmpty(bean.getList())){
//                    List<ToDayGameDataBean> listDate = new ArrayList<>();
//                    for(int i=0;i<bean.getList().size();i++){
//                        ToDayGameDataBean toDayGameDataBean = new ToDayGameDataBean();
//                        toDayGameDataBean.setGameDescribe(bean.getList().get(i).getPlayinfo()+"");
//                        toDayGameDataBean.setGameId(bean.getList().get(i).getAdid()+"");
//                        toDayGameDataBean.setGamelogo(bean.getList().get(i).getImgurl()+"");
//                        toDayGameDataBean.setGameName(bean.getList().get(i).getAdnamecut()+"");
//                        toDayGameDataBean.setGameTJ(bean.getList().get(i).getIssue()+"");
//                        toDayGameDataBean.setGameType("XW");
//                        listDate.add(toDayGameDataBean);
//                    }
//                    listTodayBean.setValue(listDate);
//                }
//
//            }
//
//            @Override
//            public void onFailure(Call<Object> call, Throwable t) {
//
//            }
//        });
//    }

    //多游
    public void getGameDuoYouData(Context mContext,int page){
        //创建Retrofit对象
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://api.ads66.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        //创建网络请求接口实例
        BaseAppApi request = retrofit.create(BaseAppApi.class);
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("media_id",DUOYOU_APP_ID);
        hashMap.put("user_id", AppContext.getInstance().getAppPreferencesHelper().getKeyValue("user_id"));
        hashMap.put("sign", DUOYOUSdkSign.getSign(mContext));
        hashMap.put("device_type","2");
        hashMap.put("page",page+"");
        hashMap.put("size","20");
        hashMap.put("device_ids", DUOYOUSdkSign.getDeviceIdsEncode(mContext));
        Log.d("getdDoYou=>", JsonUtil.toJson(hashMap)+"");
        Call<Object> call = request.gateGuoYouListData(hashMap);
        haveMore = false;
        //发送网络请求(异步)
        call.enqueue(new Callback<Object>() {
            @Override
            public void onResponse(Call<Object> call, Response<Object> response) {
                Log.d("getdDoYou", "duoyou="+JsonUtil.toJson(response));
                DuoYouBaseBean bean = (DuoYouBaseBean)JsonUtil.parseJsonToBean(JsonUtil.toJson(response.body()), DuoYouBaseBean.class);
                if(!UIUtils.isEmpty(bean) && !UIUtils.isEmpty(bean.getData()) && !UIUtils.isEmpty(bean.getData())){
                    List<ToDayGameDataBean> listDate = new ArrayList<>();
                    for(DuoYouBaseBean.DuoYouListBean dataBean:bean.getData()){
                        ToDayGameDataBean toDayGameDataBean = new ToDayGameDataBean();
                        toDayGameDataBean.setGameDescribe(dataBean.getProduct_introduction()+"");
                        toDayGameDataBean.setGameId(dataBean.getAdvert_id()+"");
                        toDayGameDataBean.setGamelogo(dataBean.getProduct_icon()+"");
                        toDayGameDataBean.setGameName(dataBean.getTitle()+"");
                        toDayGameDataBean.setGameJL("最高奖励"+dataBean.getMax_price()+"元");
                        toDayGameDataBean.setGameTJ("推荐");
                        toDayGameDataBean.setGameType("DUOYOU");
                        listDate.add(toDayGameDataBean);
                    }
                    listTodayBean.setValue(listDate);
                }
            }

            @Override
            public void onFailure(Call<Object> call, Throwable t) {
            }
        });
    }

    //鱼玩盒子
    public void getYWHZList(Context mContext){
        //创建Retrofit对象
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://yuwanapi.xinliangxiang.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        //创建网络请求接口实例
        BaseAppApi request = retrofit.create(BaseAppApi.class);
        HashMap<String, String> hashMap = new HashMap<>();

        String oaid = DevicesIDUtil.getOaid();
        if(UIUtils.isEmpty(oaid)){
            oaid = DevicesIDUtil.getIMEI(mContext);
        }
        hashMap.put("deviceIdentity", oaid+"");
        hashMap.put("appId",YWHZ_APP_ID);
       // hashMap.put("getAllData", "1");
        hashMap.put("mediaUserId", AppContext.getInstance().getAppPreferencesHelper().getKeyValue("user_id"));
        hashMap.put("sign", MD5Util.Md5String(YWHZ_AppSecret+ AppContext.getInstance().getAppPreferencesHelper().getKeyValue("user_id")+YWHZ_APP_ID));
        Log.d("getYWHZList",JsonUtil.toJson(hashMap)+"");
        Call<Object> call = request.gateYWHZData(hashMap);
        //发送网络请求(异步)
        call.enqueue(new Callback<Object>() {
            @Override
            public void onResponse(Call<Object> call, Response<Object> response) {
                Log.d("game=4", "yw="+JsonUtil.toJson(response));
                YWHZGameTaskDataBean bean = (YWHZGameTaskDataBean)JsonUtil.parseJsonToBean(JsonUtil.toJson(response.body()), YWHZGameTaskDataBean.class);
                if(!UIUtils.isEmpty(bean) && !UIUtils.isEmpty(bean.getData()) && !UIUtils.isEmpty(bean.getData())){
                    List<ToDayGameDataBean> listDate = new ArrayList<>();
                    for(int i=0;i<bean.getData().size();i++){
                        ToDayGameDataBean toDayGameDataBean = new ToDayGameDataBean();
                        toDayGameDataBean.setGameDescribe(bean.getData().get(i).getTaskDescription()+"");
                        toDayGameDataBean.setGameId(bean.getData().get(i).getTaskId()+"");
                        toDayGameDataBean.setGamelogo(bean.getData().get(i).getAppIcon()+"");
                        toDayGameDataBean.setGameName(bean.getData().get(i).getAdName()+"");
                        toDayGameDataBean.setGameJL("领奖励");
                        toDayGameDataBean.setGameTJ("推荐");
                        toDayGameDataBean.setGameType("YWHZ");
                        listDate.add(toDayGameDataBean);
                    }
                    listTodayBean.setValue(listDate);
                }

            }

            @Override
            public void onFailure(Call<Object> call, Throwable t) {

            }
        });
    }
}
